package com.anjoyo.xgh.musicshare.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class QRAct extends Activity {
	private static final int IMAGE_HALFWIDTH = 20;
	private ImageView imageView;
	private Bitmap mBitmap;
	int[] pixels = new int[2 * IMAGE_HALFWIDTH * 2 * IMAGE_HALFWIDTH];
	Context con;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		con = this;
		imageView = new ImageView(this);
		mBitmap = ((BitmapDrawable) getResources().getDrawable(
				R.drawable.ic_launcher)).getBitmap();
		Matrix m = new Matrix();
		float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
		float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();
		m.setScale(sx, sy);
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),
				mBitmap.getHeight(), m, false);
		try {
			String s = "傻逼了吧！";
			imageView.setImageBitmap(cretaeBitmap(new String(s.getBytes(),
					"ISO-8859-1")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		imageView.setBackgroundColor(Color.WHITE);
		setContentView(imageView);
	}

	private Bitmap cretaeBitmap(String str) throws WriterException {
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, 300, 300);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int halfW = width / 2;
		int halfH = height / 2;
		int[] pixels = new int[width * height];
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
						&& y > halfH - IMAGE_HALFWIDTH
						&& y < halfH + IMAGE_HALFWIDTH) {
					pixels[y * width + x] = mBitmap.getPixel(x - halfW
							+ IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
				} else if (matrix.get(x, y)) {
					pixels[y * width + x] = 0xff00b7ee;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		try {
			savezxing(bitmap, Constant.CACHE + "zxing.png");
			Toast.makeText(con, "生成的二维码保存在:mnt/sdcard/com.zxing/zxing.png", 1)
					.show();
		} catch (IOException e) {
			Toast.makeText(con, "生成二维码失败！", 1).show();
			e.printStackTrace();
		}

		return bitmap;
	}

	public void savezxing(Bitmap bitmap, String name) throws IOException {
		File file = new File(name);
		if (!file.exists()) {
			file.getParentFile().mkdirs();
			file.createNewFile();
		}
		FileOutputStream os = new FileOutputStream(file);
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
		os.close();
	}
}
